package com.yummy.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.yummy.common.BaseResponse;
import com.yummy.common.TokenUtils;
import com.yummy.dto.DishDto;
import com.yummy.dto.QueryCategoryVo;
import com.yummy.dto.QueryDishVo;
import com.yummy.entity.Category;
import com.yummy.entity.Dish;
import com.yummy.service.CategoryService;
import com.yummy.service.DishService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @author buding
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    TokenUtils tokenUtils;

    Logger logger = LoggerFactory.getLogger(EmployeeController.class);


    @RequestMapping("/dishtest")
    BaseResponse dish_test() {
        logger.info("测试");
        QueryDishVo queryDishVo = new QueryDishVo();
        queryDishVo.setCurrentPage(1);
        queryDishVo.setPageSize(5);
        queryDishVo.setStoreId(0);
        return BaseResponse.success().data("queryDishVo", queryDishVo);
    }


    @PostMapping("/queryAll")
    BaseResponse queryDish(@RequestBody QueryDishVo queryDishVo) {

        logger.info("queryDish");
        PageInfo<DishDto> queryDishVoPageInfo = dishService.queryByPageAndCondition(queryDishVo);


        //根据Dish 中的 "categoryId": 1397844263642378242,找到具体的

        return BaseResponse.success().data("pageInfo", queryDishVoPageInfo);

    }

    @PostMapping("/addDish")
    public BaseResponse addDish(@RequestBody DishDto dishDto, HttpServletRequest request) {
        logger.info(dishDto.toString());

        Integer currentStore = tokenUtils.getCurrentStore(request);
        dishDto.setStoreId(currentStore);
        boolean result = dishService.addDishDto(dishDto);
        if (result) {
            return BaseResponse.success().message("添加成功");
        }
        return BaseResponse.fail().message("添加失败");
    }

    @GetMapping("/remove")
    public BaseResponse removeDish(@RequestParam String[] ids) {
        System.out.println("ids==>" + ids);
        for (String id : ids) {
            System.out.println(id);
        }
        boolean result = dishService.removeDishs(ids);
        if (result) {
            return BaseResponse.success().message("删除成功");
        } else {
            return BaseResponse.fail().message("删除失败");
        }
    }


    @PostMapping("/editDish")
    public BaseResponse updateDish(@RequestBody Dish dish, HttpServletRequest request) throws NoSuchAlgorithmException {
        Integer currentStore = tokenUtils.getCurrentStore(request);
        logger.info(dish.toString());
        dish.setName(dish.getName());
        dish.setStoreId(currentStore);
        dish.setCategoryId(dish.getCategoryId());
        dish.setPrice(dish.getPrice());
        boolean result = dishService.editDish(dish);
        if (result) {
            return BaseResponse.success().message("修改成功");
        } else {
            return BaseResponse.fail().message("修改失败");
        }
    }

    @GetMapping("/findDishById")
    public Dish findDishById(@RequestBody String id){
        Dish dish = dishService.findDishById(id);
        return dish;
    }


    //根据分类查询菜品
    @GetMapping("/findDishByCategoryId/{categoryId}")
    BaseResponse findDishByCategoryId(@PathVariable("categoryId") Long categoryId) {
        System.out.println(categoryId);
//        String s = JSON.toJSONString("");
        List<Dish> dishByCategoryId = dishService.findDishByCategoryId(categoryId);
        return BaseResponse.success().data("data",dishByCategoryId);
    }

    @GetMapping("/setStatus/{id}/{status}")
    public BaseResponse setStatus(@PathVariable("id") String id, @PathVariable("status") String status) {
        System.out.println(id + "----" + status);
        boolean result = dishService.updateStatus(id, status);
        if (result) {
            return BaseResponse.success();
        }
        return BaseResponse.fail().data("status", dishService.findDishById(id).getStatus());
    }

}
